﻿using System;

// XNA Libs
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

// Oak Engine Libs
using OakEngine.Input;
using OakEngine.GameState;

namespace MainJumpFleet.CGame
{
    public sealed class CGame
    {

        private static readonly CGame instance = new CGame();


        // Private Memebers
         OAK_IGameState m_currentGameState;
         GraphicsDeviceManager m_Graphics;
         GraphicsDevice m_Device;
         SpriteBatch    m_SpriteBatch;
         ContentManager m_ContentManger;
         OAK_InputManager m_Input;
         double gametime;
        // Public Memeber 
         public GraphicsDevice Device { get { return m_Device; } }
         public GraphicsDeviceManager Graphics { get { return m_Graphics; } }
         public SpriteBatch SpriteBatch { get { return m_SpriteBatch; } }
         public ContentManager Content { get { return m_ContentManger; } }
         public OAK_InputManager Input { get { return m_Input; } }

        // added this to get the GameTime in my PlayerShip Update method
         public GameTime rp_gameTime;

        // Texture Manager

        static CGame()
        {
        }

        private CGame() 
        {
            m_currentGameState = null;
            m_Device = null;
            m_SpriteBatch = null;
        }

        public static CGame GetInstance()
        {
            return instance;
        }

        /*Note Check if the content is disposed bt the parent passed in, if not ill take care of things here*/
        public void Initialize(GraphicsDevice device, ref SpriteBatch batch, ref GraphicsDeviceManager graphics,  ContentManager content,ref OAK_InputManager input)
        {
            m_Device = device;
            m_SpriteBatch = batch;
            m_Graphics = graphics;
            m_ContentManger = content;
            m_Input = input;


            // After we set up our important stuff we will trigger the main menu state and go in their
            ChangeState(new MainJumpFleet.GameStates.MainMenuState());
        }


        public void Shutdown()
        {
            m_currentGameState.Exit();
            m_currentGameState = null;
        }

        public void ChangeState(OAK_IGameState currentState)
        {
            // Check if the game is in a GameState
            // If so then lets exit our current state
            if (m_currentGameState != null)
                m_currentGameState.Exit();

            //Now the last check we do if the state being passed in is valid,
            // If so then set that to our state and lets rock and roll if not then we justed exited 
            // before so thats it...where done
            if (currentState != null)
            {
                m_currentGameState = currentState;
                m_currentGameState.Enter();
            }

        }


        public void Update(GameTime time)
        {
            m_currentGameState.Input(m_Input);
            m_currentGameState.Update(time);
            // added this to get the GameTime in my PlayerShip Update method
            rp_gameTime = time;

        }

        public void Render()
        {            
            m_SpriteBatch.Begin();

            m_currentGameState.Render();

            m_SpriteBatch.End();
        }
    }
}
